home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / utility / freedos.zip / COM050.ZIP / CMDINPUT.C < prev    next >
C/C++ Source or Header  |  1996-01-17  |  11KB  |  469 lines

  1. /* 
  2.  *  CMDINPUT.C - handles command input (tab completion, history, etc.)
  3.  *
  4.  *
  5.  *
  6.  *  Comments:
  7.  *
  8.  *  01/14/95 (Tim Norman) -------------------------------------------------
  9.  *    started.
  10.  *  
  11.  *  08/08/95 (Matt Rains) -------------------------------------------------
  12.  *    i have cleaned up the source code. changes now bring this source into
  13.  *    guidelines for recommended programming practice.
  14.  *
  15.  *    i have added some constants to help making changes easier.
  16.  *
  17.  *  12/12/95 (Tim Norman) -------------------------------------------------
  18.  *    added findxy() function to get max x/y coordinates to display
  19.  *    correctly on larger screens
  20.  *
  21.  *  12/14/95 (Tim Norman) -------------------------------------------------
  22.  *    fixed the Tab completion code that Matt Rains broke by moving local
  23.  *    variables to a more global scope and forgetting to initialize them
  24.  *    when needed
  25.  *
  26.  */
  27.  
  28. #include <conio.h>
  29. #include <dos.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <dir.h>
  33.  
  34. #define D_BS      8
  35. #define D_DELETE  256+83
  36. #define D_TAB     9
  37. #define D_HOME    256+71
  38. #define D_END     256+79
  39. #define D_UP      256+72
  40. #define D_DOWN    256+80
  41. #define D_LEFT    256+75
  42. #define D_RIGHT   256+77
  43. #define D_ENTER   13
  44. #define D_ESC     27
  45. #define D_BEEP    printf("\a")
  46.  
  47. void history(int, char *); /* prototype for the command-line history */
  48.  
  49. /* this is inefficient to call each time something is changed */
  50. /* make it better */
  51. void reprint(char *str, int x, int *y, int place, int maxx, int maxy)
  52. {
  53.    int gox;
  54.    int goy;
  55.  
  56.    str[127] = 0; /* truncate in case it's too long */
  57.  
  58.    gotoxy(x, *y);
  59.  
  60.    /* figure out if it's going to scroll... */
  61.    gox = x + strlen(str);
  62.    goy = *y;
  63.  
  64.    while(gox > maxx)
  65.    {
  66.       gox -= maxx;
  67.       goy++;
  68.    }
  69.  
  70.    while(goy > maxy)
  71.    {
  72.       goy--;
  73.       (*y)--;
  74.    }
  75.  
  76.    printf(str);
  77.    clreol();
  78.  
  79.    gox = x + place;
  80.    goy = *y;
  81.  
  82.    while(gox > maxx)
  83.    {
  84.       gox -= maxx;
  85.       goy++;
  86.    }
  87.  
  88.    gotoxy (gox, goy);
  89.    return;
  90. }
  91.  
  92. /* reposition the cursor */
  93. void reposition(int curx, int cury, int place, int maxx)
  94. {
  95.    int gox;
  96.    int goy;
  97.  
  98.    gox = curx + place;
  99.    goy = cury;
  100.  
  101.    while(gox > maxx)
  102.    {
  103.       gox -= maxx;
  104.       goy++;
  105.    }
  106.  
  107.    gotoxy(gox, goy);
  108.    return;
  109. }
  110.  
  111. /* get the size of the screen */
  112. void findxy (int *maxx, int *maxy)
  113. {
  114.    struct text_info t;
  115.  
  116.    gettextinfo (&t);
  117.  
  118.    *maxx = t.screenwidth;
  119.    *maxy = t.screenheight;
  120. }
  121.  
  122. /* read in a command line */
  123. void readcommand(char *str, int maxlen)
  124. {
  125.    struct ffblk file;
  126.    int place = 0; 
  127.    int len = 0; 
  128.    int ch;
  129.    int curx; 
  130.    int cury; 
  131.    int count;
  132.    int y;
  133.    int maxx, maxy;
  134.  
  135.    /* varibles found within code */
  136.    int found_dot = 0; 
  137.    int curplace = 0; 
  138.    int start; 
  139.    int perfectmatch = 1;
  140.    char path[128]; 
  141.    char fname[14]; 
  142.    char maxmatch[13] = "";
  143.    char directory[128];
  144.  
  145.    /* find the bounds of the current screen */
  146.    findxy (&maxx, &maxy);
  147.  
  148.    curx = wherex();
  149.    cury = wherey();
  150.    str[0] = 0;
  151.  
  152.    do
  153.    {
  154.       ch = getch();
  155.  
  156.       if(ch == 0) /* special key */
  157.       {
  158.          ch = 256 + getch();
  159.       }
  160.  
  161.       switch(ch)
  162.       {
  163.          case D_BS :
  164.          {
  165.             /* delete character to left of cursor */
  166.             if(place != 0)
  167.             {
  168.                place--;
  169.  
  170.                for(count = place; count < len; count++)
  171.                {
  172.                   str[count] = str[count + 1];
  173.                }
  174.  
  175.                len--;
  176.  
  177.                if(wherex() != 1)
  178.                {
  179.                   printf ("\b \b");
  180.                }
  181.                else
  182.                {
  183.                   y = wherey();
  184.           gotoxy(maxx, y - 1);
  185.           printf(" ");
  186.           gotoxy(maxx, y - 1);
  187.                }
  188.  
  189.            reprint(str, curx, &cury, place, maxx, maxy);
  190.             }
  191.             break;
  192.          }
  193.          case D_DELETE :
  194.          {
  195.             /* delete character under cursor */
  196.             if (place != len)
  197.             {
  198.                for(count = place; count < len; count++)
  199.                {
  200.                   str[count] = str[count + 1];
  201.                }
  202.  
  203.                len--;
  204.  
  205.                if(place != 0)
  206.                {
  207.                   if(wherex() != 1)
  208.                   {
  209.                      printf("\b \b");
  210.                   }
  211.                   else
  212.                   {
  213.                      y = wherey();
  214.              gotoxy(maxx, y - 1);
  215.              printf(" ");
  216.              gotoxy(maxx, y - 1);
  217.                   }
  218.                }
  219.  
  220.            reprint (str, curx, &cury, place, maxx, maxy);
  221.             }
  222.             break;
  223.          }
  224.          case D_HOME :
  225.          {
  226.             /* goto beginning of string */
  227.             gotoxy(curx, cury);
  228.             place = 0;
  229.  
  230.             break;
  231.          }
  232.          case D_END :
  233.          {
  234.             /* goto end of string */
  235.             place = len;
  236.         reposition(curx, cury, place, maxx);
  237.              
  238.             break;
  239.          }
  240.          case D_TAB :
  241.          {
  242.         maxmatch[0] = 0;
  243.         found_dot = 0;
  244.         curplace = 0;
  245.         perfectmatch = 1;
  246.  
  247.             /* expand current file name */
  248.             if(place == len)  /* only works at end of line */
  249.             {
  250.            count = len - 1;
  251.  
  252.            while(count > 0 && str[count] != ' ') /* find front of word */
  253.            {
  254.           count--;
  255.            }
  256.  
  257.            if(str[count] == ' ')  /* if not at beginning, go forward 1 */
  258.                {
  259.                   count++;
  260.                }
  261.  
  262.                start = count;
  263.  
  264.                /* extract directory from word */
  265.                strcpy (directory, &str[start]);
  266.                curplace = strlen (directory) - 1;
  267.                while (curplace >= 0 && directory[curplace] != '\\' &&
  268.                   directory[curplace] != ':')
  269.                {
  270.                   directory[curplace] = 0;
  271.                   curplace--;
  272.                }
  273.  
  274.                strcpy(path, &str[start]);
  275.  
  276.            /* look for a . in the filename */
  277.            for(count = strlen (directory); path[count] != 0; count++)
  278.                {
  279.                   if(path[count] == '.')
  280.                   {
  281.                      found_dot = 1;
  282.                      break;
  283.                   }
  284.                }
  285.  
  286.                if(found_dot)
  287.                {
  288.                   strcat(path, "*");
  289.                }
  290.                else
  291.                {
  292.                   strcat(path, "*.*");
  293.                }
  294.  
  295.                curplace = 0; /* current fname */
  296.  
  297.                if(findfirst(path, &file, 0x3F) == 0) /* find anything */
  298.                {
  299.                   do
  300.                   {
  301.                      if(file.ff_name[0] == '.') /* ignore . and .. */
  302.                      {
  303.                         continue;
  304.                      }
  305.  
  306.                      strcpy(fname, file.ff_name);
  307.  
  308.                      if(file.ff_attrib == FA_DIREC)
  309.                      {
  310.                         strcat(fname, "\\");
  311.                      }
  312.                      else
  313.                      {
  314.                         strcat(fname, " ");
  315.                      }
  316.  
  317.                      if(!maxmatch[0] && perfectmatch)
  318.                      {
  319.                         strcpy(maxmatch, fname);
  320.                      }
  321.                      else
  322.                      {
  323.                         for (count = 0; maxmatch[count] && fname[count];count++)
  324.                         {
  325.                            if (maxmatch[count] != fname[count])
  326.                            {
  327.                               perfectmatch = 0;
  328.                               maxmatch[count] = 0;
  329.                               break;
  330.                            }
  331.                         }
  332.                      }
  333.                   }
  334.                   while(findnext(&file) == 0);
  335.  
  336.                   strcpy(&str[start], directory);
  337.                   strcat(&str[start], maxmatch);
  338.                   len = strlen(str);
  339.                   place = len;
  340.           reprint(str, curx, &cury, place, maxx, maxy);
  341.  
  342.                   if(